const app = getApp()
const imgurl2 = app.data.imgurl;
import { openProgram } from '../../utils/util.js';
import { getKjId, getUserInfo, signIn, getShare } from '../../utils/requestApi.js';

Page({
  data: {
    newsData: {},
    imgurl: app.data.imgurl,
    userInfo: null,
    userData: {}, // 会员相关信息（等级、积分、钱包等）
    showAccount: true,
    signIn: '点击签到',
    memberpoint: 0,
    showcard: false,
    showmask2: false,
    showmask3start: false,
    showmask2start: false,
    nowcreat: false,
    animationData: {}, //空动画对象属性
    kjid: null, // 控价id
    showInt: false, // 显示积分modal
    showIntAni: false, //积分modal  animation 
    points: 0, // 积分
    userRepository: [
      // { num: "42.00", text: '余额' },
      { num: 0, text: '收藏', url: 'myCollect' },
      { num: 0, text: '优惠券', url: '' },
      { num: 0, text: '积分', url: 'myIntegral' }
    ],
    myorderBox: [
      { imgurl: imgurl2 + 'icon_order_01.png', text: '待收款', num: 0, color: '#333333' },
      { imgurl: imgurl2 + 'icon_order_02.png', text: '待发货', num: 0, color: '#333333' },
      { imgurl: imgurl2 + 'icon_order_03.png', text: '待收货', num: 0, color: '#333333' },
      { imgurl: imgurl2 + 'icon_order_04.png', text: '待评价', num: 0, color: '#333333' },
      { imgurl: imgurl2 + 'icon_quanbudingdan.png', text: "全部订单", num: 0, color: '#FF3333' }
    ],
    memberCenter: [
      { imgurl: imgurl2 + 'icon_shouhuodizi.png', text: '收货地址' },
      { imgurl: imgurl2 + "icon_dingdan.png", text: '代理订单', isShow: false },
      { imgurl: imgurl2 + 'icon_fenxiaoxiashu.png', text: '代理下属', isShow: false },
      { imgurl: imgurl2 + 'apply1.png', text: '申请代理', isShow: false },
      { imgurl: imgurl2 + 'binding.png', text: '绑定代理', isShow: false },
      { imgurl: imgurl2 + 'apply2.png', text: '申请分销员', isShow: false },
      { imgurl: imgurl2 + 'commission.png', text: '我的佣金', isShow: false },
      { imgurl: imgurl2 + 'url1.png', text: '引流商城' },
      { imgurl: imgurl2 + 'url2.png', text: '控价系统', isShow: false }
    ],
  },

  //   订单栏
  Tomyorder(e) {
    var index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: `/pages/myOrder/myOrder?type=${index}`,
    })

  },
  //   收货地址栏
  clickCemberCenter(e) {
    console.log(e.currentTarget.dataset.index)
    switch (e.currentTarget.dataset.index) {
      case 0:
        // 代码块
        wx.navigateTo({
          url: '/pages/receivingAddress/receivingAddress',  // 收货地址
        })
        break;
      case 1:
        // 代码块
        wx.navigateTo({
          url: '/pages/distributeOrder/distributeOrder',   //代理订单
        })
        break;
      case 2:
        // 代码块
        wx.navigateTo({
          url: '/pages/distributeSub/distributeSub',   //代理下属
        })
        break;
      case 3:
        // 代码块
        wx.navigateTo({
          url: '/pages/askAgency/askAgency',   //申请代理
        })
        break;
      case 4:
        // 代码块
        wx.navigateTo({
          url: '/pages/bindAgency/bindAgency',   //绑定代理
        })
        break;
      case 5:
        // 代码块
        wx.navigateTo({
          url: '/pages/askDistributor/askDistributor',   //申请分销员
        })
        break;
      case 6:
        // 代码块
        wx.navigateTo({
          url: '/pages/commission/commission',   // 佣金
        })
        break;
      case 7:
        // 代码块
        openProgram({ appId: 'wx1a5e8d42120bd70f', path: 'pages/home/home', attrName: 'unid', attrValue: app.data.unionId }) // 跳转引流小程序
        break;
      case 8:
        // 代码块
        openProgram({ appId: 'wx4c251ff32916a86f', path: 'pages/index/main', attrName: 'kjid', attrValue: this.data.kjid }) // 跳转控价小程序
        break;
    }
  },

  /** 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  onShow: function () {
    this.onKj();
    this.getUserInfo();
    app.globalData.template.tabbar("tabBar", 3, this, app.data.cartNum) //0表示第一个tabba
  },

  //获取代理的控价id
  onKj() {
    getKjId().then(res => {
      if (res.data.Status == 'Success') this.setData({ kjid: res.data.Message })
    })
  },

  //获取用户信息
  getUserInfo() {
    let { userRepository, memberCenter, myorderBox } = this.data;
    getUserInfo({ 
      ShopType: 2,
      ShowGiftOrder: true
    }).then(res => {
      wx.showLoading({ title: '加载中...' })
      if (res.data.Status == 'Success') {
        wx.hideLoading();
        wx.setStorage({
          key: 'userInfo',
          data: res.data.Data,
        })
        let { Balance, points, couponsCount, IsReferral, KjId, FavoriteCount, waitFinishCount, waitPayCount, waitReviewCount, waitSendCount } = res.data.Data;
        myorderBox.forEach((item, index) => {
          if (index == 0) item.num = waitPayCount
          if (index == 1) item.num = waitSendCount
          if (index == 2) item.num = waitFinishCount
          if (index == 3) item.num = waitReviewCount
        })
        memberCenter.forEach(item => {
          if (item.text != '收货地址' && item.text != '引流商城') item.isShow = false
          // 普通会员
          if (!IsReferral) {
            if (item.text == '申请分销员' || item.text == '申请代理' || item.text == '绑定代理') item.isShow = true
          }
          // 分销员
          if (IsReferral && KjId == 0) {
            if ( item.text == '申请代理' || item.text == '绑定代理') item.isShow = true
          }
          // 代理
          if (IsReferral && KjId > 0) {
            if (item.text == '控价系统' || item.text == '代理订单' || item.text == '代理下属' || item.text == '我的佣金') item.isShow = true
          }
        })
        userRepository.forEach((item, index) => {
          if (index == 0) item.num = FavoriteCount
          if (index == 1) item.num = couponsCount
          if (index == 2) item.num = points
        })
        this.setData({ userInfo: res.data.Data, userRepository, memberCenter, myorderBox })
      }
    })
  },

  /**
   * 签到
   * */
  onSignIn: function (e) {
    if (e.currentTarget.dataset.type != 1 && wx.getStorageSync('userInfo').IsSignToday) {
      wx.showToast({ icon: 'none', title: '今天已签到' })
      return
    }
    if (e.currentTarget.dataset.type == 1) {
      this.setData({ showIntAni: false })
      setTimeout(() => {
        this.setData({ showInt: !this.data.showInt })
      }, 300)
      return
    }
    wx.showLoading({ title: '拼命签到中...' })
    signIn().then(res =>{
      wx.hideLoading();
      if (res.data.Status == 'Success') {
        this.getUserInfo();
        this.setData({ showInt: !this.data.showInt, points: res.data.points })
        setTimeout(() => {
          this.setData({ showIntAni: !this.data.showIntAni })
        }, 10)
      }else wx.showToast({ title: '签到失败' })
    })
  },
  
  // 分享
  share() {
    this.selectComponent('#share').shareInfo();
  }
})